<?php
include("inc/class.otr.php");
$otr = new OTR();

if(!$otr->checkLogin()){
	header("Location: index.php");
}

if(isset($_GET['do'])){
	if($_GET['do']=='insert'){
		$project_id = $_GET['projectid'];
		$task = $_GET['task'];
		$prio = $_GET['prio'];
		$owner = $_GET['owner'];
		$sql = "Insert into `tasks` (`related_id`, `related_table`, `task`, `owner_id`, `prio`, `ordinal`, `bit_completed`, `completed_on`)
				Values (".$project_id.", 'projects', '".$task."', ".$owner.", ".$prio.", 0, 0, '0000-00-00 00:00:00')";
		mysql_query($sql) or die("SQL Error: ".mysql_error()."<br/>".$sql);
		//echo($sql);
		echo("<script type='text/javascript'>ajaxGet('tasks.php', 'id=".$project_id."', $('div_tasks'), null);</script>");
		exit();
	}else{
		if($_GET['what']=='bit_completed'){
			$sql = "Update `tasks` Set `bit_completed`=1, `completed_on`=NOW() Where `id`=".$_GET['id'];
		} else {
			$sql = "Update `tasks` Set `".$_GET['what']."`='".mysql_escape_string($_GET['data'])."' Where `id`=".$_GET['id'];
		}
		echo($sql."<br/>");
		//$sql = mysql_escape_string($sql);
		if(mysql_query($sql) or die("SQL Error: ".mysql_error())){
			echo "Task updated!";
			exit();
		} else {
			echo "Failed updating task!";
			exit();
		}
	}
}

$users = $otr->getUsers();
$proj_id = $_REQUEST['id'];
$object = $otr->getTasks($proj_id);

?>
<div id="tasks" style="float: left;">
	<table cellspacing="0" cellpadding="1px" style="color: black;">
		<tr><td/><td>Task</td>
		<td>Owner</td>
		<td>Prio</td>
		<td>Total acc time</td>
		</tr>
<?php
	foreach($object as $o){
?>
	<tr class="<?php echo($otr->getPrioCSS($o['prio'], $o['bit_completed']));?>" style="border-bottom: black 1px dotted; padding: 2px;">
		<td>
			<input type="button" value="+" style="font-family: monospace;" onclick="subTask(this, <?php echo($o['id']);?>);" />
			<input type="hidden" id="txt_id" value="<?php echo($o['id']);?>"/>
			<input type="hidden" id="ordinal" value="<?php echo($o['ordinal']);?>" style="width: 20px;"/>
		</td>
		<td><input readonly="readonly" onblur="exitEdit(<?php echo($o['id']);?>, this);" type="text" id="txt_task" value='<?php echo($o["task"]);?>' ondblclick="goEdit(this);" style="width: 300px;"/></td>
		<td><select id="ddl_owner" style="width: 60px; border: 1px solid gray;" onchange="updateTask(<?php echo($o['id']);?>, 'owner_id', this);">
<?php
	foreach($users as $u){
		$selected = '';
		if($u['id']==$o['owner_id']) $selected = 'selected="selected"';
?>
		<option <?php echo($selected);?> value="<?php echo($u['id']);?>"><?php echo($u['initials']);?></option>
<?php
	}
?>
		</select></td>
		<td><select id="ddl_prio" style="width: 40px; border: 1px solid gray;" onchange="updateTask(<?php echo($o['id']);?>, 'prio', this);">
			<option class="prio_low" value="1" <?php if($o['prio']==1) echo('selected="selected"');?>>&nbsp;</option>
			<option class="prio_medium" value="2" <?php if($o['prio']==2) echo('selected="selected"');?>>&nbsp;</option>
			<option class="prio_high" value="3" <?php if($o['prio']==3) echo('selected="selected"');?>>&nbsp;</option>
			<option class="prio_mad" value="4" <?php if($o['prio']==4) echo('selected="selected"');?>>&nbsp;</option>
		</select></td>
		<td style="text-align: right;"><?php echo($otr->getTaskTotalTime($o['id']));?></td>
		<!-- <td style="text-align: right;"><?php //echo($o['time']);?></td> -->
<?php
	if($o['bit_completed']==1){
		//task completed
?>
		<td style="text-align: right;"><input style="border: none;" readonly="readonly" id="txt_completed" value="<?php echo($o['completed_on']);?>" /></td>
<?php
	}else{
		//task NOT completed
?>
		<td>
			<input type="button" style="background-image: url(imgs/btnCompleted.gif); background-repeat: no-repeat;" onclick="updateTask(<?php echo($o['id']);?>, 'bit_completed', 1);" />
			<input type="button" style="background-image: url(imgs/punch_v1.gif); background-repeat: no-repeat;" value="" onclick="punch(<?php echo($o['id']);?>);" />
		</td>
		<td><img src="imgs/clockv2.gif" id="img_punch_<?php echo($o['id']);?>" style="display: none; border: none;" /></td>
		<td><div id="txt_clock_<?php echo($o['id']);?>" /></td>
		<td><input style="display: none; background-color: #fff; border: 1px solid #000;" type="text" id="txt_punchnote_<?php echo($o['id']);?>" /></td>
		<td><input type="button" style="display: none;" value="!" id="btn_punchnote_<?php echo($o['id']);?>" onclick="updatePunch(this);"/></td>
<?php		
	}
?>
		</tr>
		<tr>
			<td colspan="7">
				<div id="div_subtask_<?php echo($o['id']);?>" style="display: none;">
<?php include('subtasks.php');?>
				</div>
			</td>
		</tr>
<?php
}
?>
		<tr>
			<td colspan="6" style="text-align: right;">
				<input type="button" value="add" onclick="showAddRow('addRow');"/>
			</td>
		</tr>
	<tr class="" id="addRow" style="display: none; background-color: silver;">
	<td>
	<input type="hidden" id="txt_id" value="0"/>
	<input type="hidden" id="ordinal" value="" style="width: 20px;"/>
	</td>
		<td><input type="text" id="add_txt_task" value="" style="width: 300px; border-bottom: 1px solid gray; background-color: white;"/></td>
		<td><select id="add_ddl_owner" style="width: 60px; border: 1px solid gray;">
<?php
	foreach($users as $u){
?>
			<option value="<?php echo($u['id']);?>"><?php echo($u['initials']);?></option>
<?php
	}
?>
		</select></td>
		<td colspan="2"><select id="add_ddl_prio" style="width: 60px; border: 1px solid gray;">
			<option class="prio_low" value="1">low</option>
			<option class="prio_medium" value="2">medium</option>
			<option class="prio_high" value="3">high</option>
			<option class="prio_mad" value="4">mad</option>
		</select></td>
		<td style="text-align: right;">
			<input type="button" value="save" onclick="insertNewTask();"/>
			<input type="button" value="cancel" onclick="exitAdd('addRow');"/>
		</td>
	</tr>
	</table>
</div>
<div id="div_result"/>
<script type="text/javascript">
	var oldValue;

	var punchTaskId = 0;
	var punchRunning = false;

	var goEdit = function(el){
		oldValue = el.value;
		el.removeProperty('readonly');
		el.setStyles({
			'background-color':'white'
		});
	}
	var exitEdit = function(id, el){
		el.set('readonly', 'readonly');
		el.setStyles({
			'background-color':'inherit'
		});
		if(oldValue!=el.value){
			var q = "do=update&id=" + id + "&what=task&data=" + el.value;
			ajaxGet('tasks.php', q, $('div_result'), null);
		}
	}
	var updateTask = function(id, what, el){
		var q = "do=update&id=" + id + "&what=" + what + "&data=" + el.value;
		ajaxGet('tasks.php', q, $('div_result'), null);
		if(what=='prio'){
			var css;
			switch(el.value){
				case "1":
					css = 'prio_low';
					break;
				case "2":
					css = 'prio_medium';
					break;
				case "3":
					css = 'prio_high';
					break;
				case "4":
					css = 'prio_mad';
					break;
			}
			el.getParent('tr').set('class', css);
		}
	}
	var punch = function(id){
		//user clicked another punch in-button
		if(punchTaskId==0 || id==punchTaskId){
			if(punchRunning){
				punchRunning = false;
				$('txt_punchnote_'+punchTaskId).setStyle('display', 'none');
				$('btn_punchnote_'+punchTaskId).setStyle('display', 'none');
				$('img_punch_'+punchTaskId).setStyle('display', 'none');
				ajaxGet('punch.php', 'action=update&id='+punchTaskId, $('txt_clock_'+punchTaskId), null);
				punchTaskId = 0;
			}else{
				punchRunning = true;
				//use "global"
				punchTaskId = id;
				$('txt_punchnote_'+punchTaskId).setStyle('display', '');
				$('btn_punchnote_'+punchTaskId).setStyle('display', '');
				$('img_punch_'+punchTaskId).setStyle('display', '');
				ajaxGet('punch.php', 'action=insert&id='+punchTaskId, $('txt_clock_'+punchTaskId), null);
				elapsed();
			}
		}else{
			//exit current running punch
			punchRunning = false;
			$('txt_punchnote_'+punchTaskId).setStyle('display', 'none');
			$('btn_punchnote_'+punchTaskId).setStyle('display', 'none');
			$('img_punch_'+punchTaskId).setStyle('display', 'none');
			ajaxGet('punch.php', 'action=update&id='+punchTaskId, $('txt_clock_'+punchTaskId), null);
			punchTaskId = 0;
			punch(id);
		}
	}
	var elapsed = function(){
		if(punchRunning){
			ajaxGet('punch.php', 'action=update&id='+punchTaskId, $('txt_clock_'+punchTaskId), null);
			(function(){
				elapsed();
			}).delay(60000);		
		}
	}
	var showAddRow = function(el){
		$(el).setStyle('display', '');
		//$('add_txt_task').focus();
	}
	var exitAdd = function(el){
		$(el).setStyle('display', 'none');
	}
	var insertNewTask = function(){
		var q = 'do=insert&projectid='+<?php echo($proj_id);?>+'&task='+$('add_txt_task').value+'&prio='+$('add_ddl_prio').value+'&owner='+$('add_ddl_owner').value;
		ajaxGet('tasks.php', q, null, null);
	}
	var insertNewSubTask = function(taskid){
		var q = 'do=insert&taskid='+taskid+'&task='+$('addsub_txt_task_'+taskid).value+'&prio='+$('addsub_ddl_prio_'+taskid).value+'&owner='+$('addsub_ddl_owner_'+taskid).value;
		ajaxGet('subtasks.php', q, $('div_result'), null);
	}
	var subTask = function(el, id){
		//alert(el.getNext('div'));
		var ctrl = 'div_subtask_'+id;
		var on = '';
		var btn = '-';
		if(el.value=='-'){
			on = 'none';
			btn = '+'
		}
		el.value = btn;
		$(ctrl).setStyle('display', on);
	}
	var updatePunch = function(el){
		var ctrl = el.id.replace('btn_', 'txt_');
		var note = $(ctrl).value;
		if (note.length > 0) {
			ajaxGet('punch.php', 'action=note&id=' + punchTaskId + '&note=' + note, null, null);
		}
	}
</script>